package cn.com.jslh.boarpro.main.model;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.com.jslh.boarpro.base.BaseApplication;
import cn.com.jslh.boarpro.tables.ModelTable;
import cn.com.jslh.boarpro.util.WebService;
import cn.com.jslh.boarpro.util.database.DataBaseUtil;

/**
 * 作者：周洲 on 2017/7/12 11:02
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class LoginModelImpl {
    private Context context;

    public LoginModelImpl(Context context) {
        this.context = context;
    }

    /**
     * 登陆
     *
     * @param netSuccessListener
     * @param params
     */
    public void doLogin(final WebService.NetSuccessListener netSuccessListener, final String... params) {
        new WebService.Builder(context, "login", "EASLogin")
                .addProperty("userName", params[0])
                .addProperty("password", params[1])
//                .addProperty("userName", "苏成")
//                .addProperty("password", "suc123")
                .addProperty("slnName", "eas")
                .addProperty("dcName", "LiHua")//正式服务器
//                .addProperty("dcName", "LIHUA") //测试
                .addProperty("language", "L")
                .addProperty("dbType", 1)
                .success(new WebService.NetSuccessListener() {
                    @Override
                    public void Success(String s) throws JSONException {
                        Log.d("登录s",s+"zxx");
                        String[] ss = s.split(";");
                        if (s.contains("sessionId")) {
                            for (int i = 0; i < ss.length; i++) {
                                String str = ss[i];
                                if (str.contains("sessionId")&&!str.substring(str.indexOf("=") + 1).equals("null")) {
                                    SPUtils.getInstance().put(BaseApplication.EASSESSIONID, str.substring(str.indexOf("=") + 1));//登陆成功.
//                                    HeaderProperty headerPropertyObj = new HeaderProperty("sessionId", str.substring(str.indexOf("=") + 1));
//                                    BaseApplication.headerList.add(headerPropertyObj);
                                    //请求获取用户信息
                                    getUserInfo(new WebService.NetSuccessListener() {
                                        @Override
                                        public void Success(String s) throws JSONException {
                                            Log.d("登录s",s+"zxx");
                                            netSuccessListener.Success(s);
                                        }
                                    }, params);
                                    return;
                                }else {
//                                    Toast.makeText(context, "sessionid获取失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        } else {
                            Toast.makeText(context, "用户名/密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .faild(new WebService.NetFaildListener() {
                    @Override
                    public void Faild(String str) {
                        if(!str.equals("")) {
                            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .showDialog(true)
                .post();
    }

    /**
     * 获取用户信息
     *
     * @param netSuccessListener
     * @param params
     */
    public void getUserInfo(final WebService.NetSuccessListener netSuccessListener, String... params) {
        new WebService.Builder(context, "exportUserOrgData", "WSZpigBaseDataFacade")
                .addProperty("userName", params[0])
                .success(new WebService.NetSuccessListener() {
                    @Override
                    public void Success(String s) throws JSONException {
                        JSONObject jsonObject = new JSONObject(s);
                        String flag = jsonObject.getString("flag");
                        switch (flag) {
                            case "1"://执行成功
                                JSONArray jsonArray = jsonObject.getJSONArray("array");
                                JSONObject data = jsonArray.getJSONObject(0);
                                SPUtils.getInstance().put(BaseApplication.FLINENAME, data.getString("FLINENAME"));
                                SPUtils.getInstance().put(BaseApplication.FORGNAME, data.getString("FORGNAME"));
                                SPUtils.getInstance().put(BaseApplication.FUSERID, data.getString("FUSERID"));
                                SPUtils.getInstance().put(BaseApplication.FLINEID, data.getString("FLINEID"));
                                SPUtils.getInstance().put(BaseApplication.FUSERNAME, data.getString("FUSERNAME"));
                                SPUtils.getInstance().put(BaseApplication.FORGID, data.getString("FORGID"));
                                JSONArray modelsJsoArray = jsonObject.getJSONArray("models");
                                DataBaseUtil.clearTable(context, ModelTable.class);
                                DataBaseUtil.saveJsonArrayData(context, ModelTable.class, modelsJsoArray);
                                netSuccessListener.Success("");
                                break;
                            case "0":
                                Toast.makeText(context, jsonObject.getString("warn"), Toast.LENGTH_SHORT).show();
                                break;//执行失败
                        }
                    }
                })
                .faild(new WebService.NetFaildListener() {
                    @Override
                    public void Faild(String str) {
                        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
                    }
                })
                .showDialog(true)
                .post();
    }
}
